package net.xinqushi.pthread.pthreadlocal;

/**
 * @Description:ThreadLocalMap使用“开放寻址法”中最简单的“线性探测法”解决散列冲突问题
 * @author xjie
 */
public class ThreadLocalHash {
    public static void main(String[] args) {
        ThreadLocal<Integer> local = new ThreadLocal<>();
//        local.set(1);
//        local.set(2);
//        System.out.println(local.get());
        hashCode(16);//初始化16
        hashCode(32);//后续2倍扩容
        hashCode(64);
    }

    private static final String WHITE_SPACE = " ";
    //ThreadLocal中定义的hash魔数
    private static final int HASH_INCREMENT = 0x61c88647;
    /**
     *
     * @Description 寻找散列下标（对应数组小标）
     * @param length table长度
     */
    private static void hashCode(Integer length){
        int hashCode = 0;
        for(int i=0;i<length;i++){
            hashCode = i*HASH_INCREMENT+HASH_INCREMENT;//每次递增HASH_INCREMENT
            //System.out.print("hash : " + Integer.toHexString(hashCode) + WHITE_SPACE);
            System.out.print(/*"index "*/ + ( hashCode & (length-1) ) + WHITE_SPACE);//求散列下标，算法公式
        }
        System.out.println();
    }

    // 7 14 5 12 3 10 1 8 15 6 13 4 11 2 9 0 初始的16位的完美散列
}
